package com.zjitc.service.imp;

import com.zjitc.common.ServerResponse;
import com.zjitc.dao.UserDao;
import com.zjitc.po.User;
import com.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/28 15:33
 * @description:
 */
@Service
public class UserServiceImp implements UserService {

  @Autowired
  public UserDao userDao;

  @Override
  public ServerResponse<User> login(String username, String password) {
    int count = userDao.userNameCount(username);
    if (count != 1) {
      return ServerResponse.createError("user do not exist");
    }

    // Todo. password 需要加密
    User user = userDao.findByUserNameAndPassword(username, password);
    if (user == null) {
      return ServerResponse.createError("password error");
    }

    user.setPassword(null);
    user.setQuestion(null);
    user.setAnswer(null);
    return ServerResponse.createSuccess(user);
  }
}
